﻿using AutoMapper;
using MediatR;
using ZGLXM.Domain;
using ZGLXM.ErrorCode;
using ZGLXM.Infra.Interface;
using ZGLXM.Infra.Repository;
using ZGLXM.Writh.Api.Applicantion.Command.RBAC;

namespace ZGLXM.Writh.Api.Applicantion.Handler.RBAC
{
    public class ProfessionInsertCommandHandler : IRequestHandler<ProfessionInsertCommand, ApiResult>
    {
        private readonly IProfessionRepository professionRepository;
        private readonly IMapper mapper;

        public ProfessionInsertCommandHandler(IProfessionRepository professionRepository, IMapper mapper)
        {
            this.professionRepository = professionRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> Handle(ProfessionInsertCommand request, CancellationToken cancellationToken)
        {
            var profession = mapper.Map<Profession>(request);
            var res = await professionRepository.InsertAsync(profession);
            if (res > 0)
            {
                return ApiResult.OK(profession);
            }
            else
            {
                return ApiResult.Fail("权限新增失败");
            }
        }
    }
}
